package com.micromall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.base.BaseController;
import com.micromall.common.utils.Dict;
import com.micromall.common.utils.PageInfo;
import com.micromall.model.BusiProduct;
import com.micromall.model.SysUser;
import com.micromall.service.IBusiProductService;
import com.micromall.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 登陆用户表 SYS_USER  前端控制器
 * </p>
 * @author Dante Fung
 * @since 2017-07-26
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {
    
    @Autowired private ISysUserService sysUserService;

    @Autowired
    private IBusiProductService busiProductService;


    @GetMapping("/manager")
    public String manager() {
        return "admin/sysUser/sysUserList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(SysUser sysUser, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
        ew.setEntity(sysUser);
        Page<SysUser> pages = getPage(page, rows, sort, order);
        pages = sysUserService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/sysUser/sysUserAdd";
    }

    /**
     * 添加
     * @param
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(SysUser sysUser) {
        boolean b = sysUserService.insert(sysUser);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param userCode
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String userCode) {
        SysUser sysUser = new SysUser();
        sysUser.setUserCode(userCode);
        boolean b = sysUserService.updateById(sysUser);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        SysUser sysUser = sysUserService.selectById(id);
        model.addAttribute("sysUser", sysUser);
        return "admin/sysUser/sysUserEdit";
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(SysUser sysUser) {
        boolean b = sysUserService.updateById(sysUser);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

    @RequestMapping("/do_yhq_Page")
    public String do_yhq_Page(){
        return "yhq";
    }

    @RequestMapping("/do_speak_Page")
    public String do_speak_Page(HttpServletRequest request){
        EntityWrapper<BusiProduct> ew = new EntityWrapper<BusiProduct>();
        BusiProduct busiProduct = new BusiProduct();
        busiProduct.setDataStatus(Dict.YesOrNo.Yes.getCode());
        busiProduct.setPublishStatus(Dict.YesOrNo.Yes.getCode());
        ew.setEntity(busiProduct);
        List<BusiProduct> busiProductList =  busiProductService.selectList(ew);
        request.setAttribute("busiProductList",busiProductList);
        return "/speak";
    }

    @RequestMapping("/do_member_Page")
    public String do_member_Page(){
        return "/member";
    }


    @RequestMapping("/login")
    public Object  login(HttpServletRequest request){
        //String smsCode = request.getAttribute("smsCode");
        String smsCode = request.getParameter("smsCode");
        String phoneNo = request.getParameter("phoneNo");
        HttpSession session = request.getSession();
        String session_smsCode = (String )session.getAttribute("smsCode");
        //判断该手机号是否注册
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        SysUser user = new SysUser();
        user.setUserCode("WS"+phoneNo);
        ew.setEntity(user);
        SysUser sysUser = sysUserService.selectOne(ew);
        if(null == sysUser){
            //注册该账号，并且登录
            boolean b  = sysUserService.insert(user);
            if (b) {
                SysUser newUser = sysUserService.selectOne(ew);
                session.setAttribute("cuurUser",newUser);
//                return renderSuccess("添加成功！");
                return write2Json("REGISTER_SUCCESS", Boolean.TRUE,"注册成功！且登陆成功", null);
            } else {
                return write2Json("REGISTER_FAIL", Boolean.FALSE,"注册失败", null);
            }
        }

        //验证码错误。从新登录
        if(!smsCode.equals(session_smsCode)){
            return write2Json("lOGIN_FAIL", Boolean.FALSE,"登陆失败!请重新登陆", null);
        }

        //登录该账号
        session.setAttribute("cuurUser",sysUser);
        return write2Json("lOGIN_SUCCESS", Boolean.TRUE,"登陆成功!", null);
    }
}
